package com.hhu.wangzb.server.controller;

import com.hhu.wangzb.common.entity.*;
import com.hhu.wangzb.server.service.*;
import com.hhu.wangzb.server.util.RequestParamsWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用于数据库数据导入
 */
@RestController
public class DataImportController {

    @Autowired
    private BoxChangeService boxChangeService;

    @Autowired
    private BranchGwService branchGwService;

    @Autowired
    private CollectorLineService collectorLineService;

    @Autowired
    private ComponentBracketService componentBracketService;

    @Autowired
    private FieldGroundingService fieldGroundingService;

    @Autowired
    private InverterService inverterService;

    @Autowired
    private LowAcCableService lowAcCableService;

    @Autowired
    private LowDcCableService lowDcCableService;

    @Autowired
    LowVolLineService lowVolLineService;

    @Autowired
    MainGwService mainGwService;

    @Autowired
    PlaceService placeService;

    @Autowired
    ProjectDetailService projectDetailService;

    @Autowired
    PvAreaService pvAreaService;

    @Autowired
    PvComponentService pvComponentService;

    @Autowired
    PvDistrictService pvDistrictService;

    @Autowired
    PvSpeCableService pvSpeCableService;

    @Autowired
    RegionDetailService regionDetailService;

    @RequestMapping("/save")
    public String save(String value, String tableName) throws InstantiationException, IllegalAccessException {
        if (tableName == null) return "tableName参数不能为空！";
        if (value == null) return "value参数不能为空！";
        if (tableName.equals("t_box_change")) {
            BoxChangeEntity wrap = (BoxChangeEntity) RequestParamsWrapper.wrap(value, BoxChangeEntity.class);
            boxChangeService.save(wrap);
        }
        else if (tableName.equals("t_inverter")) {
            InverterEntity wrap = (InverterEntity) RequestParamsWrapper.wrap(value, InverterEntity.class);
            inverterService.save(wrap);
        }
        else if (tableName.equals("t_branch_gw")) {
            BranchGwEntity wrap = (BranchGwEntity) RequestParamsWrapper.wrap(value, BranchGwEntity.class);
            branchGwService.save(wrap);
        }
        else if (tableName.equals("t_collector_line")) {
            CollectorLineEntity wrap = (CollectorLineEntity) RequestParamsWrapper.wrap(value, CollectorLineEntity.class);
            collectorLineService.save(wrap);
        }
        else if (tableName.equals("t_component_bracket")) {
            ComponentBracketEntity wrap = (ComponentBracketEntity) RequestParamsWrapper.wrap(value, ComponentBracketEntity.class);
            componentBracketService.save(wrap);
        }
        else if (tableName.equals("t_field_grounding")) {
            FieldGroundingEntity wrap = (FieldGroundingEntity) RequestParamsWrapper.wrap(value, FieldGroundingEntity.class);
            fieldGroundingService.save(wrap);
        }
        else if (tableName.equals("t_low_ac_cable")) {
            LowAcCableEntity wrap = (LowAcCableEntity) RequestParamsWrapper.wrap(value, LowAcCableEntity.class);
            lowAcCableService.save(wrap);
        }
        else if (tableName.equals("t_low_dc_cable")) {
            LowDcCableEntity wrap = (LowDcCableEntity) RequestParamsWrapper.wrap(value, LowDcCableEntity.class);
            lowDcCableService.save(wrap);
        }
        else if (tableName.equals("t_low_vol_line")) {
            LowVolLineEntity wrap = (LowVolLineEntity) RequestParamsWrapper.wrap(value, LowVolLineEntity.class);
            lowVolLineService.save(wrap);
        }
        else if (tableName.equals("t_main_gw")) {
            MainGwEntity wrap = (MainGwEntity) RequestParamsWrapper.wrap(value, MainGwEntity.class);
            mainGwService.save(wrap);
        }
        else if (tableName.equals("t_place")) {
            PlaceEntity wrap = (PlaceEntity) RequestParamsWrapper.wrap(value, PlaceEntity.class);
            placeService.save(wrap);
        }
        else if (tableName.equals("t_project_detail")) {
            ProjectDetailEntity wrap = (ProjectDetailEntity) RequestParamsWrapper.wrap(value, ProjectDetailEntity.class);
            projectDetailService.save(wrap);
        }
        else if (tableName.equals("t_pv_area")) {
            PvAreaEntity wrap = (PvAreaEntity) RequestParamsWrapper.wrap(value, PvAreaEntity.class);
            pvAreaService.save(wrap);
        }
        else if (tableName.equals("t_pv_component")) {
            PvComponentEntity wrap = (PvComponentEntity) RequestParamsWrapper.wrap(value, PvComponentEntity.class);
            pvComponentService.save(wrap);
        }
        else if (tableName.equals("t_pv_district")) {
            PvDistrictEntity wrap = (PvDistrictEntity) RequestParamsWrapper.wrap(value, PvDistrictEntity.class);
            pvDistrictService.save(wrap);
        }
        else if (tableName.equals("t_pv_spe_cable")) {
            PvSpeCableEntity wrap = (PvSpeCableEntity) RequestParamsWrapper.wrap(value, PvSpeCableEntity.class);
            pvSpeCableService.save(wrap);
        }
        else if (tableName.equals("t_region_detail")) {
            RegionDetailEntity wrap = (RegionDetailEntity) RequestParamsWrapper.wrap(value, RegionDetailEntity.class);
            regionDetailService.save(wrap);
        }

        return "ok！";
    }

}
